package com.konsung.ksdc.manager.community;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.GroupNetBean;
import com.konsung.ksdc.common.database.DatabaseHelper;
import com.konsung.ksdc.config.NetConfig;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 创建者     kgh
 * 创建时间   2016/6/14 18:29
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public class GroupListActivity extends Activity {
    @Bind(R.id.lv_group)
    ListView lvGroup;
    @Bind(R.id.img_back)
    ImageView back;
    private GroupListAdapter groupListAdapter;
    private List<GroupNetBean> groups;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initComponent();
    }

    /**
     * 初始化组件
     */
    private void initComponent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (NetConfig.currentUser == null || NetConfig.currentUser.getUserId() == null) {
            return;
        }
        groups = DatabaseHelper.getHelper(this)
                .getRuntimeExceptionDao(GroupNetBean.class)
                .queryForEq("ownerId", NetConfig.currentUser.getUserId());
        groupListAdapter = new GroupListAdapter(groups);
        lvGroup.setAdapter(groupListAdapter);
        lvGroup.setOnItemClickListener(new AdapterView
                .OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                GroupNetBean group = groups.get(position);
                Intent intent = new Intent(GroupListActivity.this, GroupChatActivity.class);
                intent.putExtra("topicName", group.getGroupId()); //群id
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
